package org.util.tag;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.util.string.StringUtils;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * 标签功能工具类
 * 
 * @Title: TagUtils.java
 * @Description:培训师平台
 * @Copyright: Copyright (c) 2011
 * @Company:北京千家在线
 * @author miaoyouhu
 * @date Jul 6, 2011 10:34:52 AM
 * @version V1.0
 */
public class TagUtils {
	static Logger logger = Logger.getLogger(TagUtils.class.getName()); // 日志工具类

	/**
	 * 在网页上显示特定语言的信息
	 * 
	 * @param pageContext
	 *            页面上下文
	 * @param str
	 *            页面显示内容
	 */
	public static void display(PageContext pageContext, Object str)
			throws IOException {
		pageContext.getOut().print(str);
	}

	/**
	 * 获得值栈里面的值 ;
	 * 
	 * @param property
	 * @return
	 */
	public static Object getValue(String property) {
		ValueStack vs = ServletActionContext.getContext().getValueStack();
		Object obj = vs.findValue(property);
		if (obj != null) {
			vs = null;
			return obj;
		}
		return "";
	}

	/**
	 * 关键字分隔
	 * 
	 * @param keywords
	 *            分隔的字符串
	 * 
	 * @return
	 */
	public static String getKeywords(String keywords) {

		StringBuffer result = new StringBuffer();
		if (StringUtils.is_null(keywords)) {
			String[] keys = keywords.split(",");
			for (int i = 0; i < keys.length; i++) {
				String value = keys[i];
				result.append("<a href=\""
						+ ServletActionContext.getRequest().getContextPath()
						+ "/tags/" + value + "/\">" + value + "</a>");
			}
		}
		return result.toString();
	}

	/**
	 * 关键字分隔
	 * 
	 * @param keywords
	 *            分隔的字符串
	 * 
	 * @return
	 */
	public static String getKeywordsTwo(String keywords) {
		StringBuffer result = new StringBuffer();
		if (StringUtils.is_null(keywords)) {
			String[] keys = keywords.split(",");
			int count = 0;
			for (int i = 0; i < keys.length; i++) {
				count++;
				String value = keys[i];
				if (count == keys.length) {
					result.append("<a href=\""
							+ ServletActionContext.getRequest()
									.getContextPath() + "/tags/" + value
							+ "/\">" + value + "</a>");
				} else {
					result.append("<a href=\""
							+ ServletActionContext.getRequest()
									.getContextPath() + "/tags/" + value
							+ "/\">" + value + "</a>&nbsp;|&nbsp;");
				}

			}
		}
		return result.toString();
	}

	/**
	 * 处理字体大小;
	 * 
	 * @return
	 */
	public static String textSize(String text, int size) {
		int num = 2;
		if (size >= 1 && size <= 5) {
			num = 2;
		} else if (size > 5 && size <= 20) {
			num = 3;
		} else if (size > 20 && size <= 40) {
			num = 4;
		} else if (size > 40) {
			num = 5;
		}
		return "<font size='" + num + "'>" + text + "</font>";
	}
}
